package controllers;

import game.Player;
import game.Scoreboard;

import java.util.ArrayList;

import paintball.PaintballPlayer;

/**
 * Player Controller. This class will manage all connected players.
 * @author Twisty
 *
 */
public class PlayerController {
	//List of connected players
	private static ArrayList<PaintballPlayer> players = new ArrayList<PaintballPlayer>();
	
	//The current game state
	public static int currentGameState;
	
	/**
	 * Private constructor, cannot be called.
	 */
	private PlayerController(){
	}
	
	/**
	 * @return - List of player
	 * @throws Exception - Can throw an exception when a player disconnects while the list is being called
	 */
	/**
	 * Get all connected players.
	 * @return All of the connected players.
	 * @throws Exception
	 */
	public static ArrayList<PaintballPlayer> getPlayers() throws Exception{
		return players;
	}
	
	/**
	 * Add a player.
	 * @param p	The player to add.
	 * @return The recently added player.
	 */
	public static Player addPlayer(PaintballPlayer p){		
		p.setRotation(Math.random()*360);
		players.add(p);
		Scoreboard.addPlayer(p.getPlayerName());
		return p;
	}
	
	/**
	 * Remove a player from the list.
	 * @param player	The player to remove from the list.
	 */
	public static void removePlayer(PaintballPlayer player){
		Scoreboard.removeScore(player.getPlayerName());
		try{
		players.remove(player);
		}catch(Exception e){
			
		}
	}
	
	/**
	 * Get a certain player.
	 * @param playerName	The player name.
	 * @return The player (if found). Otherwise null.
	 */
	public static Player getPlayer(String playerName){
		for(PaintballPlayer p: players){
			if(p.getPlayerName().equalsIgnoreCase(playerName)){
				return p;
			}
		}
		return null;
	}
}
